var DWZ=parent.DWZ;
$(document).ready(function(){	
//	getList();	
	getNavDemo();
});

//获取Sign列表
function getNavDemo(){
	var url = "/demo/navigation";
//	var sel = $("#typeSelectDom");
//	sel.html("");
	parent.DWZ.get(url,function(json){
		if(!json) return;
		var data=json.result||[];
		$.each(data,function(i,d){
			var op = new Option(d.value,d.sign);
			$(op).attr("action",d.name+"/category");
			sel[0].options.add(op);
		});	
	});
}

//获取Sign对应的分组列表
function selectSign(sel){
	var url=$(sel.options[sel.selectedIndex]).attr("action");
	var listSelect = $("#listSelectDom");
	listSelect.html("");
	var html=[];
	parent.DWZ.get(url,function(json){
		if(!json) return;
		var data = json.result||[];
		if(data.length==0){
			listSelect.append("<a href='javascript:void(0);'>无相关列表</a>");
		}
		$.each(data,function(i,d){
			var temp = $("#listSelectTemplet").html();
			temp = temp.replace(/\${name}/g,d.name).replace(/\${category_id}/g,d.category_id)
					.replace(/\${sign}/g,d.sign);
			html.push(temp);
		});
		listSelect.append(html.join(""));
		$("#listSelectDom").children("li").children("a").first().click();
	});
}

var choiceLists={};

/**
 * 执行选择操作
 */
function choice(){
	if(callback)callback(choiceLists);
}

function selectList(sel){
	var url="/demo/list";
	var sign=$(sel).attr("sign");
	parent.DWZ.get(url,{sign:sign},function(list){
		var listContainer = $("#list");
		listContainer.html("");
		var data = list.result||[];
		if(data.length==0){
			$(listContainer).append("无相关模板");
		}
		$.each(data,function(i,d){
			var pars={
				pagesize:d.param.pagesize||10,
				pageno:d.param.pageno||1,
				cond:d.param.cond||"",
				fields:d.param.fields||"",
				orderfield:d.param.orderfield||""
			};
			url = "category/"+$(sel).attr("categoryId")+"/entity";
			parent.DWZ.get(url,pars,function(entity){
				var entitys = entity.result||[];
//				if(entitys.length==0){
//					$(listContainer).html("").append("无相关数据");
//				}
				bindList(listContainer,d,entitys);
			});
		});
	});
}

//绑定列表数据
function bindList(listContainer,list,entitys){
	var templet = list.templet;
	if(entitys==undefined || entitys.length==0){
		$(listContainer).html("").append("无相关数据");
		return ;
	}
	var html=[];
	$.each(entitys,function(i,entity){
		var row = templet.bind(entity);
		html.push(row);
	})
	var container = list.container.replace(/\$\{container}/,html.join(""));
	var checkbox = $("<div name='head' style='text-align: left;'><span><input type='checkbox' name='list' value='"+list.list_id+"' /></span></div>");
	checkbox.appendTo(listContainer);
	$(container).appendTo(listContainer);
	checkbox.find("input[type=checkbox]").click(function(e){
		if(this.checked){
			choiceLists["list_id"+list.list_id]={
				data:list,
				demo:container
			}
			$(this).parents("div[name=head]").next().css("border","red 1px solid");
		}else{
			delete choiceLists["list_id"+list.list_id];
			$(this).parents("div[name=head]").next().css("border","red 0px solid");
		}
	});
}